//
//  EnteryViewController.m
//  Audit
//
//  Created by cc on 15-3-4.
//  Copyright (c) 2015年 cc. All rights reserved.
//

#import "EnteryViewController.h"
#import "CarCell.h"
#import "DefectCell.h"
#import "UIImageView+WebCache.h"
#import "AddDefectViewController.h"
#import "PIDrawerViewController.h"
#import "AddDefectViewController.h"
#import "AddCarViewController.h"
#import "CarDetailViewController.h"
#import "DefectDetailViewController.h"
#import "AppDelegate.h"
#import "FlashViewController.h"
#import "GetVinViewController.h"
#import "AppDelegate.h"

@interface EnteryViewController ()<UITableViewDataSource,UITableViewDelegate,LCSwipeTableViewCellDelegate,UIAlertViewDelegate>
{
    IBOutlet UITableView *_carsTableView;
    IBOutlet UITableView *_defectTableView;
    
    IBOutlet UIView *_guideAddCarView;
    IBOutlet UIView *_guideAddDefectView;
    CarCell *_selectedCarCell;
}
@end

@implementation EnteryViewController

//-(void) dealloc
//{
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:NOTIFICATION_IS_LOAD_CAR_LIST object:nil];
//}

- (void)viewDidLoad {
    [super viewDidLoad];

    AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
    
    delegate.isLoadCarList=NO;
    delegate.isNoNetWork=NO;
    
    [[NSNotificationCenter defaultCenter]  addObserver:self selector:@selector(setIsLoadCarLsit:) name:NOTIFICATION_IS_LOAD_CAR_LIST object:nil];
    
    [[NSNotificationCenter defaultCenter]  addObserver:self selector:@selector(guideBegin) name:NOTIFICATION_HOME_GUIDE_OVER object:nil];
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark- Actions
-(void)guideBegin
{
    AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
    if (delegate.showGuide) {
        [self.view addSubview:_guideAddCarView];
    }
}

-(IBAction)guideAddCarAction:(id)sender
{
    [_guideAddCarView removeFromSuperview];
    AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
    if (delegate.showGuide) {
        [self.view addSubview:_guideAddDefectView];
    }
    
}

-(IBAction)guideAddDefectAction:(id)sender
{
    [_guideAddDefectView removeFromSuperview];
    
    AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
    
    delegate.showGuide = NO;
    delegate.showFlash = NO;
}

#pragma mark -
#pragma mark notification Methods
- (void)setIsLoadCarLsit:(id)sender
{
    AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
    
    delegate.isLoadCarList=YES;
}
#pragma mark- UItableview delegate&datasource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == _carsTableView) {
        return [self.listCar count];
    }
    else{
        if (self.selectedCar) {
            return [self.selectedCar.listDefect count];
        }
        else
        {
            return 0;
        }
    }
    
//        if (tableView == _carsTableView) {
//            return 5;
//        }
//        else{
//            return 5;
//        }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    if (tableView == _carsTableView) {
        //车辆列表
        
        
        CarCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CarCell"];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"CarCell" owner:self options:nil] lastObject];
        }
        
        
        Car *cellCar=[self.listCar objectAtIndex:[indexPath row]];
        
        cell.carType.text = cellCar.seriesDesc;
//        cell.carConfig.text = cellCar.modelDesc;
        cell.carVIN.text = cellCar.vin;
        cell.carLevel.text = cellCar.level;
        cell.carTotalScore.text = cellCar.totalScore;
        
//        cell.carType.text = @"11";
//        cell.carConfig.text = @"11";
//        cell.carVIN.text = @"11";
        
        cell.tag = 10000 + indexPath.row;
        cell.delegate = self;
        
        UIColor *color = [UIColor whiteColor];
        cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
        cell.selectedBackgroundView.backgroundColor = color;
        
        return cell;
    }
    else{
        
        //缺陷列表
        DefectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DefectCell"];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"DefectCell" owner:self options:nil] lastObject];
        }
        cell.delegate = self;
        
        [cell.line setImage:[UIImage imageNamed:@"line_defect.png"]];
        
        cell.weight.text = ((Defect *)[self.selectedCar.listDefect objectAtIndex:[indexPath row]]).weightDesc;
        
        cell.weight.backgroundColor = [GetColor getWeightColor:cell.weight.text];
        
        cell.title.text = ((Defect *)[self.selectedCar.listDefect objectAtIndex:[indexPath row]]).part2Desc;
        cell.defectInfo.text = ((Defect *)[self.selectedCar.listDefect objectAtIndex:[indexPath row]]).defectDesc;
        cell.tag = 20000 + indexPath.row;
        
        if (((Defect *)[self.selectedCar.listDefect objectAtIndex:[indexPath row]]).uploadedFile) {
            [cell.picture setImage:((Defect *)[self.selectedCar.listDefect objectAtIndex:[indexPath row]]).uploadedFile];
        }
        else
        {
            [cell.picture setImage:[UIImage imageNamed:@"defaultImg.png"]];
        }
        return cell;
        
    }
    return nil;
}

// 长按进入详情页
- (void)tableViewCellDidLongPressed:(LCSwipeTableViewCell *)cell
{
    if ([cell isKindOfClass:[CarCell class]]) {
        
        self.selectedCar = [self.listCar objectAtIndex:cell.tag-10000];
        
        CarDetailViewController *vc = [[CarDetailViewController alloc] init];
        vc.selectedCar = self.selectedCar;
        [self presentViewControllerInFullRect:vc];
    }
    else if ([cell isKindOfClass:[DefectCell class]])
    {
        self.selectedDefect = [self.selectedCar.listDefect objectAtIndex:cell.tag-20000];
        DefectDetailViewController *vc = [[DefectDetailViewController alloc] init];
        vc.selectedDefect = self.selectedDefect;
        [self presentViewControllerInFullRect:vc];
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 122;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _carsTableView) {
        
        _selectedCarCell = (CarCell*)[tableView cellForRowAtIndexPath:indexPath];
        self.selectedCar = [self.listCar objectAtIndex:[indexPath row]];
        
        [_defectTableView reloadData];
    }
    else
    {
        self.selectedDefect = [self.selectedCar.listDefect objectAtIndex:[indexPath row]];
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (tableView == _carsTableView) {
        UIView *footview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _carsTableView.frame.size.width, 130)];
        //    footview.backgroundColor = [UIColor purpleColor];
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, _carsTableView.frame.size.width-20, 120)];
        [btn setImage:[UIImage imageNamed:@"button_add.png"] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(addNewCar:) forControlEvents:UIControlEventTouchUpInside];
        [footview addSubview:btn];
        return footview;
    }
    else if (tableView == _defectTableView) {
        UIView *footview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _defectTableView.frame.size.width, 130)];
        //    footview.backgroundColor = [UIColor purpleColor];
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, _defectTableView.frame.size.width-20, 120)];
        [btn setImage:[UIImage imageNamed:@"button_add.png"] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(addCarDefect:) forControlEvents:UIControlEventTouchUpInside];
        [footview addSubview:btn];
        return footview;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (tableView == _carsTableView) {
        return 130;
    }
    return 180;
}


#pragma mark LCSwipeTableViewCellDelegate

- (void)tableViewCell:(LCSwipeTableViewCell *)cell didTriggerRightUtilityButtonWithIndex:(NSInteger)index {
    if ([cell isKindOfClass:[CarCell class]]) {
        
        Car *cellCar =[self.listCar objectAtIndex:cell.tag-10000];
        
        self.editCar = cellCar;

        //车辆列表
        switch (index) {
            case 0:
            {
                //编辑
                AddCarViewController *vc=[[AddCarViewController alloc] init];
                vc.doSth=@"edit";
                vc.currentCar=cellCar;
                [self presentViewControllerInFullRect:vc];
                
                __block int selectedIndex = 0;
                
                vc.editCar=^(Car *editCar){
                    for (int i = 0;i<self.listCar.count;i++) {
                        if ([((Car *)[self.listCar objectAtIndex:i]).vehicleId isEqualToString:editCar.vehicleId]) {
                            [self.listCar removeObjectAtIndex:i];
                            [self.listCar insertObject:editCar atIndex:i];
                            selectedIndex = i;
                            break;
                        }
                    }
                    [_carsTableView reloadData];
                    
                    [_carsTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:selectedIndex inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
                    
                    self.selectedCar = [self.listCar objectAtIndex:selectedIndex];
                    
                    [_defectTableView reloadData];
                    
//                    [_selectedCarCell setSelected:YES];
                };
                
                _selectedCarCell = (CarCell*)[_carsTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
                [_selectedCarCell setSelected:YES];
            }
                break;
            case 1:
            {
                //删除
                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"确认删除吗？" preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                    
                }];
                
                UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                    //删除
//                    NSLog(@"删除车辆。。。。\n");
                    NSArray *keyArr   = [NSArray arrayWithObjects:@"vehicleId",nil];
                    NSArray *valueArr = [NSArray arrayWithObjects:self.editCar.vehicleId,nil];
                    
                    [gAlert showLoading:nil delegate:nil];
                    [CommunicateWithService communicateWithService:keyArr value:valueArr apiName:@"vhcDelete.do" finishSelector:@selector(deleteCarFinished:) failSelector:@selector(deleteCarFailed:) delegateObj:self];
                    
                }];
                [alertController addAction:cancelAction];
                [alertController addAction:okAction];
                
                
                [self presentViewController:alertController animated:YES completion:nil];
                
//                UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"确认删除吗？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
//                alert.tag=101;
//                [alert show];
            }
                break;
            default:
                break;
        }
    }
    else if ([cell isKindOfClass:[DefectCell class]])
    {
        Defect *cellDefect =[self.selectedCar.listDefect objectAtIndex:cell.tag-20000];
        
        self.editDefect=cellDefect;
        
        switch (index) {
            case 0:
            {
                //编辑
                AddDefectViewController *vc=[[AddDefectViewController alloc] init];
                vc.isFirst = YES;
                vc.doSth=@"edit";
                vc.currentDefect=cellDefect;
                vc.selectedCar=self.selectedCar;
                [self presentViewControllerInFullRect:vc];
                
                vc.editDefect=^(Defect *editDefect){
                    for (int i=0;i<self.selectedCar.listDefect.count;i++) {
                        if ([((Defect *)[self.selectedCar.listDefect objectAtIndex:i]).auditId isEqualToString:editDefect.auditId]) {
                            [self.selectedCar.listDefect removeObjectAtIndex:i];
                            [self.selectedCar.listDefect insertObject:editDefect atIndex:i];
                            break;
                        }
                    }
                    [_defectTableView reloadData];
                    
                    NSMutableArray *valueArr = [NSMutableArray array];
                    valueArr = [NSMutableArray arrayWithObjects:self.selectedCar.vin, nil];
                    
                    NSMutableArray *keyArr = [NSMutableArray array];
                    keyArr   = [NSMutableArray arrayWithObjects:@"vin", nil];
                    
                    [gAlert showLoading:nil delegate:nil];
                    
                    [CommunicateWithService communicateWithService:keyArr value:valueArr apiName:@"queryVhc.do" finishSelector:@selector(changeCarFinished:) failSelector:@selector(changeCarFailed:) delegateObj:self];
                };
            }
                break;
            case 1:
            {
                //删除
                
                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"确认删除吗？" preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                    
                }];
                
                UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                    //删除
                    
                    NSArray *keyArr   = [NSArray arrayWithObjects:@"auditId",nil];
                    NSArray *valueArr = [NSArray arrayWithObjects:self.editDefect.auditId,nil];
                    
                    [gAlert showLoading:nil delegate:nil];
                    [CommunicateWithService communicateWithService:keyArr value:valueArr apiName:@"vhcDefectDelete.do" finishSelector:@selector(deleteDefectFinished:) failSelector:@selector(deleteDefectFailed:) delegateObj:self];
                    
                }];
                [alertController addAction:cancelAction];
                [alertController addAction:okAction];
                
                
                [self presentViewController:alertController animated:YES completion:nil];
                
//                UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"确认删除吗？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
//                alert.tag=102;
//                [alert show];
            }
                break;
            default:
                break;
        }
    }
}

- (BOOL)tableViewCellShouldHideUtilityButtonsOnSwipe:(LCSwipeTableViewCell *)cell {
    return YES;
}

#pragma mark deleteCar
-(void)deleteCarFinished:(ASIHTTPRequest *)request
{
    [gAlert dismiss];
    if (request.responseStatusCode == 400) {
        //textView.text = @"Invalid code";
        NSLog(@"Invalid code");
    } else if (request.responseStatusCode == 403) {
        NSLog(@"Code already used");
        //textView.text = @"Code already used";
    } else if (request.responseStatusCode == 200) {
        for (int i=0;i<self.listCar.count;i++) {
            NSDictionary *strDic=[CommunicateWithService getDataDic:request.responseString];//解析json 2 dic
            
            NSString *flag=[strDic objectForKey:@"resultCode"];//服务器返回代码
            
            NSString *msg=[strDic objectForKey:@"resultValue"];//服务器返回提示消息
            
//            NSDictionary *strDicData=(NSDictionary *)[strDic objectForKey:@"resultData"];//服务器返回数据字典
            
            if ([flag isEqualToString:@"1001"]) {
                for (int i=0;i<self.listCar.count;i++) {
                    if ([((Car *)[self.listCar objectAtIndex:i]).vehicleId isEqualToString:self.editCar.vehicleId]) {
                        [self.listCar removeObjectAtIndex:i];
                        break;
                    }
                }
                [_carsTableView reloadData];
                
                if (self.listCar.count > 0) {
                    if ([self.editCar.vin isEqualToString:self.selectedCar.vin])
                    {
                        self.selectedCar = (Car *)[self.listCar objectAtIndex:0];
                        
                        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
                        
                        [_carsTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
                        
                        [_defectTableView reloadData];
                    }
                    else
                    {
                        for (int i=0;i<self.listCar.count;i++) {
                            if ([((Car *)[self.listCar objectAtIndex:i]).vehicleId isEqualToString:self.selectedCar.vehicleId]) {
                                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
                                
                                [_carsTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
                                break;
                            }
                        }
                        
                    }
                }
                else
                {
                    self.selectedCar = nil;
                    
                    [_defectTableView reloadData];
                }
            }
            else if ([flag isEqualToString:@"1002"])
            {
                [gAlert show:msg inView:self.view delegate:nil];
            }
        }
    }
}

-(void)deleteCarFailed:(ASIHTTPRequest *)request
{
    [gAlert dismiss];
    [NetworkCheck checkNetwork:self];
}

#pragma mark deleteDefect
-(void)deleteDefectFinished:(ASIHTTPRequest *)request
{
    [gAlert dismiss];
    if (request.responseStatusCode == 400) {
        //textView.text = @"Invalid code";
        NSLog(@"Invalid code");
    } else if (request.responseStatusCode == 403) {
        NSLog(@"Code already used");
        //textView.text = @"Code already used";
    } else if (request.responseStatusCode == 200) {
        for (int i=0;i<self.listCar.count;i++) {
            NSDictionary *strDic=[CommunicateWithService getDataDic:request.responseString];//解析json 2 dic
            
            NSString *flag=[strDic objectForKey:@"resultCode"];//服务器返回代码
            
            NSString *msg=[strDic objectForKey:@"resultValue"];//服务器返回提示消息
            
//            NSDictionary *strDicData=(NSDictionary *)[strDic objectForKey:@"resultData"];//服务器返回数据字典
            
            if ([flag isEqualToString:@"1001"]) {
                for (int i=0;i<self.selectedCar.listDefect.count;i++) {
                    if ([((Defect *)[self.selectedCar.listDefect objectAtIndex:i]).auditId isEqualToString:self.editDefect.auditId]) {
                        [self.selectedCar.listDefect removeObjectAtIndex:i];
                        break;
                    }
                }
                [_defectTableView reloadData];
                
                NSMutableArray *valueArr = [NSMutableArray array];
                valueArr = [NSMutableArray arrayWithObjects:self.selectedCar.vin, nil];
                
                NSMutableArray *keyArr = [NSMutableArray array];
                keyArr   = [NSMutableArray arrayWithObjects:@"vin", nil];
                
                [gAlert showLoading:nil delegate:nil];
                
                [CommunicateWithService communicateWithService:keyArr value:valueArr apiName:@"queryVhc.do" finishSelector:@selector(changeCarFinished:) failSelector:@selector(changeCarFailed:) delegateObj:self];
            }
            else if ([flag isEqualToString:@"1002"])
            {
                [gAlert show:msg inView:self.view delegate:nil];
            }
        }
    }
}

-(void)deleteDefectFailed:(ASIHTTPRequest *)request
{
    [gAlert dismiss];
    [NetworkCheck checkNetwork:self];
}

#pragma mark - alert
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag==101) {
        if (buttonIndex==1) {
            NSArray *keyArr   = [NSArray arrayWithObjects:@"vehicleId",nil];
            NSArray *valueArr = [NSArray arrayWithObjects:self.selectedCar.vehicleId,nil];
            
            [gAlert showLoading:nil delegate:nil];
            [CommunicateWithService communicateWithService:keyArr value:valueArr apiName:@"vhcDelete.do" finishSelector:@selector(deleteCarFinished:) failSelector:@selector(deleteCarFailed:) delegateObj:self];
        }
    }
    else
    {
        if (buttonIndex==1) {
            NSArray *keyArr   = [NSArray arrayWithObjects:@"auditId",nil];
            NSArray *valueArr = [NSArray arrayWithObjects:self.selectedDefect.auditId,nil];
            
            [gAlert showLoading:nil delegate:nil];
            [CommunicateWithService communicateWithService:keyArr value:valueArr apiName:@"vhcDefectDelete.do" finishSelector:@selector(deleteDefectFinished:) failSelector:@selector(deleteDefectFailed:) delegateObj:self];
        }
    }
}
//添加车辆
-(void)addNewCar:(id)sender
{
//    [gAlert show:@"asdfasdfdf" delegate:nil];
    
    AddCarViewController *vc=[[AddCarViewController alloc] init];
    vc.doSth=@"add";
    [self presentViewControllerInFullRect:vc];
    
    vc.addCar=^(Car *newCar){
        if (!self.listCar) {
            self.listCar=[NSMutableArray array];
        }
        
        newCar.listDefect = [NSMutableArray array];
        
        [self.listCar insertObject:newCar atIndex:0];
        [_carsTableView reloadData];
        
        self.selectedCar=[self.listCar objectAtIndex:0];
        
        [_defectTableView reloadData];
        
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        
        [_carsTableView scrollToRowAtIndexPath:indexPath
                              atScrollPosition:UITableViewScrollPositionTop animated:YES];
        [_carsTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
    };
}

//添加缺陷
-(void)addCarDefect:(id)sender
{
    if (self.selectedCar) {
    
        AddDefectViewController *vc = [[AddDefectViewController alloc] init];
        vc.selectedCar=self.selectedCar;
        [self presentViewControllerInFullRect:vc];
        
        vc.addDefect=^(Defect *newDefect){
            if (!self.selectedCar.listDefect) {
                self.selectedCar.listDefect=[NSMutableArray array];
            }
            [self.selectedCar.listDefect addObject:newDefect];
            [_defectTableView reloadData];
            
            NSMutableArray *valueArr = [NSMutableArray array];
            valueArr = [NSMutableArray arrayWithObjects:self.selectedCar.vin, nil];
            
            NSMutableArray *keyArr = [NSMutableArray array];
                keyArr   = [NSMutableArray arrayWithObjects:@"vin", nil];

            [gAlert showLoading:nil delegate:nil];
            
            [CommunicateWithService communicateWithService:keyArr value:valueArr apiName:@"queryVhc.do" finishSelector:@selector(changeCarFinished:) failSelector:@selector(changeCarFailed:) delegateObj:self];
        };
    }
    else
    {
        [gAlert show:@"还没有选择车辆哦" delegate:nil];

    }
}

-(void)changeCarFinished:(ASIHTTPRequest *)request
{
    [gAlert dismiss];
    if (request.responseStatusCode == 400) {
        //textView.text = @"Invalid code";
        NSLog(@"Invalid code");
    } else if (request.responseStatusCode == 403) {
        NSLog(@"Code already used");
        //textView.text = @"Code already used";
    } else if (request.responseStatusCode == 200) {
        NSDictionary *strDic=[CommunicateWithService getDataDic:request.responseString];//解析json 2 dic
        
        //        NSLog(@"%@",request.responseString);
        
        NSString *flag=[strDic objectForKey:@"resultCode"];//服务器返回代码
        
        NSString *msg=[strDic objectForKey:@"resultValue"];//服务器返回提示消息
        
        NSDictionary *strDicData=(NSDictionary *)[strDic objectForKey:@"resultData"];//服务器返回数据字典
        
        //        self.tableDataList = [NSMutableArray array];
        
        if ([flag isEqualToString:@"1001"]) {
            NSArray *carList = [strDicData objectForKey:@"content"];
            
//            if (carList.count == 1) {
//                Car * getCar = [[Car alloc] initWithInfo:(NSDictionary *)[carList objectAtIndex:0]];
//                
//                getCar.listDefect = self.selectedCar.listDefect;
//                
//                for (int i = 0 ; i < self.listCar.count; i++) {
//                    if ([self.selectedCar isEqual:(Car *)[self.listCar objectAtIndex:i]]) {
//                        [self.listCar replaceObjectAtIndex:i withObject:getCar];
//                        [_carsTableView reloadData];
//                        
//                        [_carsTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]  animated:NO scrollPosition:UITableViewScrollPositionNone];
//                        break;
//                    }
//                }
//            }
            if (carList.count == 1) {
                Car * getCar = [[Car alloc] initWithInfo:(NSDictionary *)[carList objectAtIndex:0]];
                
                getCar.listDefect = [NSMutableArray array];
                
                getCar.listDefect = [NSMutableArray arrayWithArray:self.selectedCar.listDefect];
                
                for (int i = 0 ; i < self.listCar.count; i++) {
                    if ([self.selectedCar.vin isEqualToString:((Car *)[self.listCar objectAtIndex:i]).vin]) {
                        [self.listCar replaceObjectAtIndex:i withObject:getCar];
                        [_carsTableView reloadData];
                        
                        [_carsTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]  animated:NO scrollPosition:UITableViewScrollPositionNone];
                        
                        break;
                    }
                }
            }

        }
        else if ([flag isEqualToString:@"1002"])
        {
            
            [gAlert show:msg inView:self.view delegate:nil];
        }
    }
}

-(void)changeCarFailed:(ASIHTTPRequest *)request
{
    [gAlert dismiss];
    //    [self hideWaitFromView:self.view animated:YES];
    [NetworkCheck checkNetwork:self];
}

@end
